package com.lib.torment.androidlib.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class DevicesUtils {
    public static final String TAG = "DevicesUtils";

    /**
     * 获取设备imsi号
     */
    public static String getImsi(Context context) {
        String imeiString = "";

        try {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            imeiString = telephonyManager.getSubscriberId();
        } catch (Exception e) {
            LogUtils.debug(TAG, "获取设备imsi号失败");
        }

        if (imeiString == null) {
            return "";
        }

        return imeiString;
    }

    /**
     * 获取设备imei号
     */
    public static String getImei(Context context) {
        String imeiString = "";

        try {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            imeiString = telephonyManager.getDeviceId();
        } catch (Exception e) {
            LogUtils.debug(TAG, "获取设备imei号失败");
        }

        if (imeiString == null) {
            return "";
        }

        return imeiString;
    }

//    /**
//     * 获取mac地址
//     */
//    public static String getMac(Context context) {
//        String mac = "";
//        try {
//            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//            WifiInfo info = wifi.getConnectionInfo();
//            mac = info.getMacAddress();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        if (mac == null) {
//            return "";
//        }
//
//        return mac;
//    }

    /**
     * 获取屏幕宽度
     */
    public static int getDisplayWidth(Context context) {
        Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);

        return dm.widthPixels;
    }

    /**
     * 获取屏幕高度
     */
    public static int getDisplayHeight(Context context) {
        Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);

        return dm.heightPixels;
    }

    /**
     * 获取屏幕尺寸
     *
     * @return 0:width，1:height
     */
    public static int[] getDisplaySize(Context context) {
        Display display = ((WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        return new int[]{dm.widthPixels, dm.heightPixels};
    }

    /**
     * 判断是否锁屏状态
     *
     * @return true:屏幕处于亮的状态，false：屏幕处于暗的状态
     */
    public static boolean isScreenOn(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }

    /**
     * 获取版本号
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取版本名称
     */
    public static String getVersionName(Context context) {
        String versionName = "";
        try {
            // 获取软件版本名称
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }
}
